package com.alstudio.ui.module.main.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.ui.module.economy.UserPointsActivity;
import com.alstudio.ui.module.economy.UserWalletActivity;
import com.alstudio.ui.module.main.MainActivity;
import com.alstudio.ui.module.notice.UserNoticeActivity;
import com.alstudio.ui.module.props.PropsActivity;
import com.alstudio.ui.module.setting.SystemSettingActivity;
import com.alstudio.ui.module.user.UserHomePageAcitivty;
import com.alstudio.ui.module.voip.PhoneRecordActivity;
import com.alstudio.utils.android.color.ALColorUtils;
import com.alstudio.utils.asyncTask.ALAsycTask;
import com.alstudio.utils.asyncTask.ALAsyncTaskCallback;
import com.alstudio.utils.user.UserIconsUtil;
import com.alstudio.view.ViewUtils;
import com.alstudio.view.tableview.ALBasicListItem;
import com.alstudio.view.tableview.ALTableView;
import com.alstudio.view.tableview.ALTableView.ALTableViewClickListener;
import com.alstudio.view.tableview.ViewItem;
import com.loovee.imaohu.R;

/**
 * 我的界面
 * 
 * @author alonso lee
 * 
 */
public class MeFragment extends Fragment implements ALTableViewClickListener {

	private ALTableView tableView;
	private ArrayList<Intent> intents = new ArrayList<Intent>();

	private ALBasicListItem userInfo;
	private ALBasicListItem userPhone;
	private ALBasicListItem userMessage;
	private ALBasicListItem userWallet;
	private ALBasicListItem userPoint;
	private ALBasicListItem userProp;
	private ALBasicListItem userSetting;

	private int missedCallHitoryCount = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.me, container, false);
		initTableView(v);
		return v;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onStart()
	 */
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		showUserInfo();
		refreshNewNoticeItem();
	}

	public void refreshNewNoticeItem() {
		loadNewNotice();
	}

	/**
	 * 
	 * @param parent
	 */
	private void initTableView(View parent) {
		tableView = (ALTableView) parent.findViewById(R.id.aLTableView1);
		tableView.setClickListener(this);

		// 用户资料,用固定大小的头像的布局
		userInfo = new ALBasicListItem(getActivity(), true);
		showUserInfo();
		ViewItem i = new ViewItem(userInfo);
		tableView.addViewItem(i);

		Intent intent = new Intent(getActivity(), UserHomePageAcitivty.class);
		intents.add(intent);

		tableView.addSegmentations(R.layout.default_blank_list_segmentation);

		// 用户电话
		userPhone = new ALBasicListItem(getActivity());
		userPhone.title.setText(R.string.TxtUserPhone);
		userPhone.setDrawable(R.drawable.me_icon_phone);
		userPhone.setItemCountColor(ALColorUtils.getColor(
				ALLocalEnv.getLocalEnv(), R.color.white));
		userPhone.setItemCountBgRes(R.drawable.friend_mutual_icon_massage);

		i = new ViewItem(userPhone);
		tableView.addViewItem(i);
		intent = new Intent(getActivity(), PhoneRecordActivity.class);
		intents.add(intent);

		// 信箱
		userMessage = new ALBasicListItem(getActivity());
		userMessage.title.setText(R.string.TxtUserMessage);
		userMessage.setDrawable(R.drawable.me_icon_message);
		userMessage.setItemCountColor(ALColorUtils.getColor(
				ALLocalEnv.getLocalEnv(), R.color.white));
		userMessage.setItemCountBgRes(R.drawable.friend_mutual_icon_massage);

		i = new ViewItem(userMessage);
		tableView.addViewItem(i);
		intent = new Intent(getActivity(), UserNoticeActivity.class);
		intents.add(intent);
		// 分段符
		tableView.addSegmentations(R.layout.default_blank_list_segmentation);

		// 钱包
		userWallet = new ALBasicListItem(getActivity());
		userWallet.title.setText(R.string.TxtUserWallet);
		userWallet.setDrawable(R.drawable.me_icon_wallet);
		i = new ViewItem(userWallet);
		tableView.addViewItem(i);
		intent = new Intent(getActivity(), UserWalletActivity.class);
		// intent = new Intent(getActivity(),
		// MyConsumptionRecordsActivity.class);
		intents.add(intent);

		// 积分
		userPoint = new ALBasicListItem(getActivity());
		userPoint.title.setText(R.string.TxtUserPoint);
		userPoint.setDrawable(R.drawable.me_icon_credit);

		i = new ViewItem(userPoint);
		tableView.addViewItem(i);
		intent = new Intent(getActivity(), UserPointsActivity.class);
		intents.add(intent);
		// 道具
		userProp = new ALBasicListItem(getActivity());
		userProp.title.setText(R.string.TxtUserProp);
		userProp.setDrawable(R.drawable.me_icon_shop);

		i = new ViewItem(userProp);
		tableView.addViewItem(i);
		intent = new Intent(getActivity(), PropsActivity.class);
		intents.add(intent);

		// 分段符
		tableView.addSegmentations(R.layout.default_blank_list_segmentation);

		// 设置
		userSetting = new ALBasicListItem(getActivity());
		userSetting.title.setText(R.string.TxtMainSettingTitle);
		userSetting.setDrawable(R.drawable.me_icon_set);
		userSetting.setItemCountBgRes(R.drawable.friend_mutual_icon_massage);
		userSetting.setItemCountColor(ALColorUtils.getColor(
				ALLocalEnv.getLocalEnv(), R.color.white));
		if (ALLocalEnv.getLocalEnv().isNewVersionAlert()) {
			userSetting.setItemCount("new");
		} else {
			userSetting.setItemCount("");
		}

		i = new ViewItem(userSetting);
		tableView.addViewItem(i);
		intent = new Intent(getActivity(), SystemSettingActivity.class);
		intents.add(intent);

		tableView.commit();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.tableview.ALTableView.ALTableViewClickListener#onClick
	 * (int)
	 */
	@Override
	public void onClick(int index) {
		// TODO Auto-generated method stub
		Intent i = intents.get(index);
		startActivity(i);
	}

	private void showUserInfo() {
		ALLocalEnv.getLocalEnv().displayImagePluginServerlet(
				ALLocalEnv.getLocalEnv().getMyProfile().getThumbAvatar(),
				userInfo.image,
				ALLocalEnv.getLocalEnv().getRoundImageDisplayOption());
		userInfo.title.setText(ALLocalEnv.getLocalEnv().getMyProfile()
				.getNick());
		userInfo.subtitle.setText(ALLocalEnv.getLocalEnv().getMyProfile()
				.getAge()
				+ " "
				+ ALLocalEnv.getLocalEnv().getMyProfile().getProvince()
				+ " " + ALLocalEnv.getLocalEnv().getMyProfile().getJid());

		UserIconsUtil.setupIconsLayout(userInfo.img_layout, ALLocalEnv
				.getLocalEnv().getMyProfile());
		ViewUtils.showView(userInfo.subtitle);
	}

	public void loadNewNotice() {
		loadMissedCallHistoryCount();
		loadMissedSystemAndNoteMsgCount();
	}

	/*********************************** 读取数据库中未接来电 ***************************************/
	// 讀取數據庫
	private ALAsyncTaskCallback loadmissedCallHistoryCallback = new ALAsyncTaskCallback() {

		@Override
		public Object onTaskStart(Object obj) {
			// TODO Auto-generated method stub
			missedCallHitoryCount = ALLocalEnv.getLocalEnv().getDatabase()
					.getUnReadCallHistory();
			return null;
		}

		@Override
		public Object onTaskFinish(Object obj) {
			// TODO Auto-generated method stub
			userPhone.setItemCount(missedCallHitoryCount);
			return null;
		}

		@Override
		public Object onTaskCancel() {
			// TODO Auto-generated method stub
			return null;
		}
	};

	/**
	 * 讀取未接来电
	 * 
	 * @param msg
	 */
	private void loadMissedCallHistoryCount() {
		// ALAsycTask loadMissedCallHistoryTask = new ALAsycTask(
		// loadmissedCallHistoryCallback);
		// loadMissedCallHistoryTask.execute("");
		userPhone.setItemCount(((MainActivity) getActivity())
				.getMissedCallCount());
	}

	private void loadMissedSystemAndNoteMsgCount() {
		userMessage.setItemCount(((MainActivity) getActivity())
				.getMissedSystemMsgCount()
				+ ((MainActivity) getActivity()).getMissedAudioMsgCount());
	}

}
